{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### QBS和QBE的数理形式定义\n",
    "\n",
    "**QBS（Query By String）** ：通过文本字符串查询，即从给定的文本字符串查询图像数据集中的所有实例。在数学上，可以形式化为找到数据集中所有与查询字符串`q`相似或匹配的图像`I`，即求解$argmax_{I \\in D} sim(embed(q), embed(I))$，其中`D`是数据集，`embed`是嵌入函数，`sim`是相似度函数。\n",
    "\n",
    "**QBE（Query By Example）** ：通过图像示例查询，即从给定的图像示例查询数据集中所有相似的图像实例。形式化为找到数据集中所有与查询图像$I_q$相似或匹配的图像`I`，即求解$argmax_{I \\in D} sim(embed(I_q), embed(I))$。\n",
    "\n",
    "在这两种查询中，`embed`函数用于将文本字符串或图像映射到一个向量空间，而`sim`函数用于计算向量之间的相似度（如欧氏距离、余弦相似度等）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "QBS Best Match: [ 1.37675084 -0.75790483 -1.83401381  0.13692703  0.62468261  0.8301226\n",
      " -1.51247034  0.38831476  1.09744918  0.16391491]\n",
      "QBE Best Match: [ 1.77720835  0.36729088  1.83047891  0.2298951   0.04052362 -0.2809466\n",
      " -0.21216129  0.07504434  1.07128388  0.27265863]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 模拟的嵌入函数\n",
    "def embed(input_data):\n",
    "    # 这里使用随机向量作为嵌入的模拟，实际中应该使用更复杂的模型\n",
    "    return np.random.randn(10)  # 假设嵌入到一个10维空间\n",
    "\n",
    "# 计算欧氏距离作为相似度度量\n",
    "def euclidean_similarity(vec1, vec2):\n",
    "    return -np.linalg.norm(vec1 - vec2)  # 注意这里取负号，因为优化问题通常是求最大值\n",
    "\n",
    "# QBS函数\n",
    "def qbs(query_string, dataset):\n",
    "    query_embedding = embed(query_string)  # 这里假设查询字符串也能被嵌入\n",
    "    best_match = None\n",
    "    max_sim = float('-inf')\n",
    "    for data in dataset:\n",
    "        data_embedding = embed(data)  # 假设数据集元素可以被嵌入\n",
    "        sim = euclidean_similarity(query_embedding, data_embedding)\n",
    "        if sim > max_sim:\n",
    "            max_sim = sim\n",
    "            best_match = data\n",
    "    return best_match\n",
    "\n",
    "# QBE函数\n",
    "def qbe(query_image, dataset):\n",
    "    query_embedding = embed(query_image)\n",
    "    best_match = None\n",
    "    max_sim = float('-inf')\n",
    "    for data in dataset:\n",
    "        data_embedding = embed(data)\n",
    "        sim = euclidean_similarity(query_embedding, data_embedding)\n",
    "        if sim > max_sim:\n",
    "            max_sim = sim\n",
    "            best_match = data\n",
    "    return best_match\n",
    "\n",
    "# 模拟数据集和查询\n",
    "dataset = [np.random.randn(10) for _ in range(100)]  # 假设有100个数据项\n",
    "query_string = \"example_query\"  # 示例查询字符串\n",
    "query_image = np.random.randn(10)  # 示例查询图像\n",
    "\n",
    "# 执行查询\n",
    "best_match_qbs = qbs(query_string, dataset)\n",
    "best_match_qbe = qbe(query_image, dataset)\n",
    "\n",
    "print(\"QBS Best Match:\", best_match_qbs)\n",
    "print(\"QBE Best Match:\", best_match_qbe)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于上述代码使用了随机生成的数据和嵌入，因此无法直接展示QBS和QBE在实际应用中的具体效果。然而，可以想象在实际应用中，`embed`函数可能会使用深度学习模型（如CNN用于图像，RNN或BERT用于文本）来提取有意义的特征表示。然后，这些表示可以用于计算查询与数据集中元素之间的相似度，从而找到最相关的匹配项。\n",
    "\n",
    "在实际应用中，查询结果（即`best_match_qbs`和`best_match_qbe`）将对应于数据集中的特定图像或文本条目，这些条目与查询在语义上最为接近。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
